package io.dshubs.archives.controller.search;

import io.dshubs.archives.entity.vo.ElasticSearchResponse;
import io.dshubs.archives.utils.ElasticSearchUtils;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.Map;

/**
 * @author tan 2021/8/20
 **/
@Api(tags = "ElasticSearch全文检索")
@RestController
@RequestMapping("/elastic-search")
public class ElasticSearchController {


    @Autowired
    private ElasticSearchUtils elasticSearchUtils;

    @ApiOperation("查询数据")
    @LogOperation("档案检索:warehouse/imitate:/img/icon-ocr-identify.png:page")
    @GetMapping
    public Result<PageData<Map<String, Object>>> searchData(@ApiIgnore @RequestParam Map<String, Object> params) throws IOException {
        return new Result<PageData<Map<String, Object>>>().ok(
                elasticSearchUtils.searchListData(String.valueOf(SecurityUser.getTenantCode()), params,"title"));
    }

    @ApiOperation("添加数据")
    @PostMapping
    public Result add(@RequestBody  ElasticSearchResponse elasticSearchResponse) throws IOException {
        elasticSearchUtils.addData(elasticSearchResponse,String.valueOf(SecurityUser.getTenantCode()));
        return new Result<>();
    }


    @ApiOperation("创建索引")
    @PostMapping("/index")
    public Result addIndex(String index) throws IOException {
        if(index == null) {
            index = String.valueOf(SecurityUser.getTenantCode());
        }
        elasticSearchUtils.createIndex(index);
        return new Result();
    }


}
